﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Module.Venues
{
    public partial class UpdateVenues_Result : System.Web.UI.Page
    {
        private int userId;
        protected string ModuleId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Upgrade"]))
            {
                Upgrade();
            }
            BindHTML();
        }

        private void Upgrade()
        {
            string strHTML = Cache["updatevenuesresult"].ToString();
            string key = "VenuesId";
            string venuesIdString = Request.QueryString[key];
            if (!string.IsNullOrEmpty(venuesIdString))
            {
                int venuesId = int.Parse(venuesIdString);
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                Model.Module_Main module = BLLModule.GetModel(venuesId);
                Jy123.Model.User_Main user = BLLUser.GetModel(module.FUser.Value);
                Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                string msg = string.Empty;
                if (module.FJType.Value == 24)
                {
                    Jy123.Model.TCenter_Main TCenter = BLLTCenter.GetModel(venuesId);
                    Jy123.Model.Public_Level tLevel = BLLLevel.GetModel(TCenter.FLevel.Value + 1);

                    Jy123.BLL.TCenter.TModule BLLTModule = new Jy123.BLL.TCenter.TModule();


                    if (BLLTModule.UpgradeModule(TCenter, tLevel.UpgradeMoney.Value, user))
                    {
                        msg = "<script>$(function(){$.moonbox.ok('升级成功!');})</script>";
                    }
                    else
                    {
                        msg = "<script>$(function(){$.moonbox.error('升级失败!');})</script>";
                    }
                }
                else
                {
                    Jy123.Model.Venues_Main venues = BLLVenues.GetModel(venuesId);
                    Jy123.Model.Public_Level level = BLLLevel.GetModel(venues.FLevel.Value + 1);

                    Jy123.BLL.Venues.VModule BLLVModule = new Jy123.BLL.Venues.VModule();


                    if (BLLVModule.UpgradeModule(venues, level.UpgradeMoney.Value, user))
                    {
                        msg = "<script>$(function(){$.moonbox.ok('升级成功!');})</script>";
                    }
                    else
                    {
                        msg = "<script>$(function(){$.moonbox.error('升级失败!');})</script>";
                    }
                }

                Response.Write(msg);
            }
        }

        private void BindHTML()
        {
            string strHTML = Cache["updatevenuesresult"].ToString();
            string key = "VenuesId";
            string venuesIdString = Request.QueryString[key];
            if (!string.IsNullOrEmpty(venuesIdString))
            {
                int venuesId = int.Parse(venuesIdString);
                ModuleId = venuesIdString;

                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                BLL.Module_Main BLLModule = new Jy123.BLL.Module_Main();
                Model.Module_Main module = BLLModule.GetModel(venuesId);
                Jy123.Model.User_Main user = BLLUser.GetModel(module.FUser.Value);

                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();

                userId = user.ID;
                strHTML = strHTML.Replace("$Cash$", (Convert.ToInt32(user.Cash.Value)).ToString());
                if (module.FJType.Value == 24)
                {
                    Jy123.BLL.TCenter_Main BLLTCenter = new Jy123.BLL.TCenter_Main();
                    Jy123.Model.TCenter_Main TCenter = BLLTCenter.GetModel(venuesId);
                    Jy123.Model.Public_Level tLevel = BLLLevel.GetModel(TCenter.FLevel.Value);
                    strHTML = strHTML.Replace("$VenuesName$", TCenter.TName);

                    BindLevelHtml(strHTML, tLevel);
                }
                else
                {
                    Jy123.BLL.Venues_Main BLLVenues = new Jy123.BLL.Venues_Main();
                    Jy123.Model.Venues_Main venues = BLLVenues.GetModel(venuesId);
                    Jy123.Model.Public_Level level = BLLLevel.GetModel(venues.FLevel.Value);
                    strHTML = strHTML.Replace("$VenuesName$", venues.VName);

                    BindLevelHtml(strHTML, level);
                }

            }
        }

        private void BindLevelHtml(string html, Jy123.Model.Public_Level cLevel)
        {
            int cLevelNumber = cLevel.LevelNumber.Value;
            int upLevelNumber = cLevelNumber + 1;
            string strWhere = string.Format("FJType={0} AND LevelNumber = {1}", cLevel.FJType.Value, upLevelNumber);
            Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
            List<Jy123.Model.Public_Level> listLevel;
            listLevel = BLLLevel.GetModelList(strWhere);
            bool isMaxLevel = (listLevel.Count == 0);
            string modelString = cLevel.Model;
            if (string.IsNullOrEmpty(modelString))
            {
                modelString = "/Images/Module/QC2playground.jpg";
            }
            html = html.Replace("$currentLevelImgSrc$", modelString);
            html = html.Replace("$CurrentVenuesName$", cLevel.LName);

            Jy123.Web.AppCode.Module appModule = new Jy123.Web.AppCode.Module();
            string strLi = Cache["updatevenuesli"].ToString();
            string currentString = appModule.BindAnnexeString(strLi, cLevel);
            html = html.Replace("$LeftLi$", currentString.ToString());
            if (!isMaxLevel)
            {
                Jy123.Model.Public_Level uLevel = listLevel[0];
                modelString = uLevel.Model;
                if (string.IsNullOrEmpty(modelString))
                {
                    modelString = "/Images/Module/QC2playground.jpg";
                }
                html = html.Replace("$upgradeLevelImgSrc$", modelString);
                html = html.Replace("$UpgradeVenuesName$", uLevel.LName);

                currentString = appModule.BindAnnexeString(strLi, uLevel);
                html = html.Replace("$RightLi$", currentString.ToString());
                Jy123.BLL.Public_ULevel BLLULevel = new Jy123.BLL.Public_ULevel();
                html = html.Replace("$UpgradeMoney$", (Convert.ToInt32(uLevel.UpgradeMoney.Value)).ToString());
                Jy123.BLL.Module.Module BLLModule = new Jy123.BLL.Module.Module();
                bool canUpgrade = BLLModule.LevelUpByMoney(userId, uLevel.UpgradeMoney.Value);
                if (canUpgrade)
                {
                    html = html.Replace("$ClickEvent$", "fillhtml('updateContentDiv','/Module/Venues/UpdateVenues_Result.aspx?Upgrade=true&VenuesId='+$('#venuesHidden').val()+'&'+ Math.random())");
                }
                else
                {
                    html = html.Replace("$ClickEvent$", "$.moonbox.warning('您的金币不足！')");
                }
            }
            else
            {
                html = html.Replace("$upgradeLevelImgSrc$", "/Images/Module/QC2playground.jpg");
                html = html.Replace("$UpgradeVenuesName$", "待定");
                html = html.Replace("$UpgradeMoney$", "待定");
                html = html.Replace("$RightLi$", "待定");
                html = html.Replace("$ClickEvent$", "$.moonbox.warning('目前没有更高等级，敬请期待！')");
            }
            Response.Write(html);
        }
    }
}
